<?php
class SaveUserInfo extends Action {
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$user = rqsts ( array ('uid', 'name', 'email', 'passwd', 'passwd1' ), true );		
		$userModel = new CoreUserModel ();		
		if (empty ( $user ['passwd'] )) { // 修改时如果没设置密码，则不修改密码
			unset ( $user ['passwd'], $user ['passwd1'] );
		}
		//验证用户数据的合法性
		$valid = $this->validate ( 'userinfo', $user, $userModel );
		if ($valid === true) { //用户数据合法
			unset ( $user ['passwd1'] );
			if (isset ( $user ['passwd'] )) {
				$user ['passwd'] = md5 ( $user ['passwd'] );
			}
			$rst = $userModel->save ( $user );
			if (! empty ( $rst )) {
				sess_add ( 'page_tip_info', '<strong>恭喜,</strong>用户账户修改成功.' );
				sess_add ( 'edit_user_info_rst', true );
			} else {
				sess_add ( 'edit_user_info_rst', false );
				sess_add ( 'page_tip_info', '<strong>出错啦!</strong>' . $userModel->last_error_msg () );
			}
		} else {
			sess_add ( 'edit_user_info_rst', false );
			sess_add ( 'user_error', $valid );
		}
		$user ['uname'] = rqst ( 'uname' );
		sess_add ( 'user_info', $user );
		Response::redirect ( $this->referer );
	}
}